<template>
  <div class="measurement">
    <div class="measurement-header">
      <span>量测</span>
      <i class="el-icon-circle-close close-btn" @click="close"></i>
    </div>

    <div class="measurement-body">
      <div class="tool-list">
        <div class="list-item" v-for="(item, index) in measurementList" :key="index" :class="{'is-select': item.isCheck}" @click="clickMeasurement(item)">
          <img :src="require(`@/assets/image/measurement/${item.name}.png`)" alt="">
          <span>{{item.name}}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import measure from '@/utils/measure.js';
import draw from '@/utils/draw';
export default {
  data() {
    return {
      measurementList: [
        {
          name: '空间距离',
          isCheck: false
        },
        {
          name: '贴地距离',
          isCheck: false
        },
        {
          name: '三角测量',
          isCheck: false
        },
        {
          name: '水平面积',
          isCheck: false
        },
        {
          name: '贴地面积',
          isCheck: false
        },
        {
          name: '体积测量',
          isCheck: false
        },
        {
          name: '清除',
          isCheck: false
        }
      ]
    };
  },

  methods: {
    clickMeasurement(option) {
      this.measurementList.forEach(item => {
        item.isCheck = item.name == option.name ? !item.isCheck : false;
      });

      if (option.isCheck) {
        switch (option.name) {
          case '空间距离':
            this._Measure.distance();
            break;

          case '贴地距离':
            this._Measure.distanceClampSurface();
            break;

          case '三角测量':
            this._Measure.heightTriangle();
            break;

          case '水平面积':
            this._Measure.area();
            break;

          case '贴地面积':
            this._Measure.areaSurface();
            break;

          case '体积测量':
            this._Draw.drawPolygonGraphics({
              style: {
                material: Cesium.Color.fromCssColorString("#7FFF00"),
                clampToGround: true,
                width: 3
              },
              callback: (pos, entity) => {
                this._Measure.areaVolume(pos);
              }
            });
            break;

          case '清除':
            document.body.style.cursor = "default";
            this._Measure.clearDraw();
            this._Draw.clearDraw();
            option.isCheck = false;
            break;

          default:
            break;
        }
      }
    },

    close() {
      this.$emit('close');
    }
  },

  created() {
    this.viewer = this.Global.viewer;
    this._Measure = new measure(this.viewer);
    this._Draw = new draw(this.viewer);
  },

  beforeDestroy() {
    document.body.style.cursor = "default";
    this._Measure.destroy();
    this._Draw.destroy();
  }
}
</script>

<style lang="less" scoped>
.measurement {
  position: fixed;
  right: 10px;
  top: 94px;
  width: 225px;
  padding: 15px 0;
  box-sizing: border-box;
  border-radius: 3px;
  background-color: #333;
  z-index: 999;
  .measurement-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 10px 15px 10px;
    box-sizing: border-box;
    border-bottom: 1px solid #565656;
    font-size: 14px;
    color: #b4b4b4;
    .close-btn {
      cursor: pointer;
    }
  }

  .measurement-body {
    .tool-list {
      display: flex;
      justify-content: space-between;
      align-content: center;
      flex-wrap: wrap;
      column-gap: 10px;
      padding: 15px 10px 0 10px;
      box-sizing: border-box;
      .list-item {
        display: flex;
        justify-content: space-around;
        align-items: center;
        padding: 5px 10px;
        margin-bottom: 10px;
        box-sizing: border-box;
        border-radius: 5px;
        background-color: #474747;
        font-size: 13px;
        color: #e6e6e6;
        cursor: pointer;
        &:hover {
          background-color: #6f99e4;
        }

        img {
          width: 20px;
          height: 20px;
        }
      }

      .is-select {
        background-color: #6f99e4;
      }
    }
  }
}
</style>